const fs = require('fs')

const template = require("art-template")

function httpReadFile(path,response){
    fs.readFile(path,(error,data)=>{
        if(error){
            response.setHeader("Content-Type","text/plain; charset=UTF-8")
            response.write("资源加载错误")
            response.end(error)
        }else {
            response.end(data)
        }
    })
}

function httpReadFileWithData(path,response,data){
    fs.readFile(path,(error,fileData)=>{
        if(error){
            response.setHeader("Content-Type","text/plain; charset=UTF-8")
            response.write("资源加载错误")
            response.end(error)
        }else {
            response.end(template.render(fileData.toString(),data))
        }
    })
}

module.exports = {
    httpReadFile,
    httpReadFileWithData
}